/*! ******************************************************************************
 *
 * Created on 2022年2月24日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.yzbdl.lanius.core.net;

import java.io.InputStream;
import java.util.List;

import org.pentaho.di.core.exception.KettleExceptionHandler;
import org.yzbdl.lanius.core.exception.LaniusServerErrorException;

import com.google.common.collect.Lists;

import cn.hutool.http.HttpException;
import cn.hutool.http.cookie.GlobalCookieManager;
import cn.hutool.http.server.SimpleServer;

/**
 * Lanius Http Util
 *
 * @author hs@yzbdl.ac.cn
 */
public class HttpUtil {

    private static List<KettleExceptionHandler> exceptionHandlers = Lists.newArrayList();

    public static Integer port;

    public static SimpleServer server;

    private static final int timeout = 3 * 1000;

    /**
     * 初始化网络服务
     * <p>
     * 1, 设置cookie manager
     */
    public static void init() {
        GlobalCookieManager.setCookieManager(new CookieManager());
    }

    public static void addExceptionHandler(KettleExceptionHandler handler) {
        exceptionHandlers.add(handler);
    }

    /**
     * 通过get请求获取返回的流, 仅可用于获取一些小的文本或文件，例如验证码, 如果需要下载文件请使用DownloadService
     *
     * @param url
     * @return
     * @throws LaniusServerErrorException
     */
    public static InputStream getStream(String url) throws LaniusServerErrorException {
        return execute(HttpRequest.get(url)).bodyStream();
    }

    /**
     * 发起get请求，返回文本
     *
     * @param url
     * @return
     * @throws LaniusServerErrorException
     * @throws HttpException
     */
    public static String getString(String url) throws HttpException, LaniusServerErrorException {
        return execute(HttpRequest.get(url)).body();
    }

    /**
     * 提交json内容请求
     * @param url
     * @param bodyParam
     * @return
     */
    public static HttpResponse postJson(String url, String bodyParam) {
        // 设置超时时间，防止卡死
        HttpRequest.setGlobalTimeout(timeout);
        return execute(HttpRequest.postJson(url).body(bodyParam));
    }

    /**
     * 提交json内容请求
     * @param url
     * @param bodyParam
     * @return
     */
    public static HttpResponse postJsonWithAuth(String url, String token, String bodyParam) {
        // 设置超时时间，防止卡死
        HttpRequest.setGlobalTimeout(timeout);
        return execute(HttpRequest.postJsonWithAuth(url, token).body(bodyParam));
    }

    /**
     *
     * @param url
     * @param bodyParam
     * @return
     * @throws LaniusServerErrorException
     */
    public static String postString(String url, String bodyParam) {
        return execute(HttpRequest.post(url).body(bodyParam)).body();
    }

    private static HttpResponse execute(HttpRequest request) {
        try {
            return request.execute();
        } catch (LaniusServerErrorException e) {
            for (KettleExceptionHandler exceptionHandler : exceptionHandlers) {
                exceptionHandler.handle(e);
            }
            return null;
        }
    }
}
